package cn.dnui_wwt524.dnuifood_16110100524.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.CollectStatus;
import cn.dnui_wwt524.dnuifood_16110100524.controller.FoodInfoActivity;
import cn.dnui_wwt524.dnuifood_16110100524.controller.FoodListActivity;
import cn.dnui_wwt524.dnuifood_16110100524.model.UserModel;

public class CollectAdapter extends RecyclerViewAdapter<CollectAdapter.ViewHolder>{
    public CollectAdapter(Context context, List data,int layoutResource) {
        super(context, data, layoutResource);
    }


    @NonNull
    @Override
    public CollectAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.from(parent.getContext()).inflate(R.layout.collect_item,parent,false);
        return new CollectAdapter.ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(@NonNull CollectAdapter.ViewHolder viewHolder, final int position) {
        final CollectStatus entity = (CollectStatus)data.get(position);
        if (null == entity)
            return;
        CollectAdapter.ViewHolder holder = (CollectAdapter.ViewHolder) viewHolder;
        if (entity.getFlag().equals("0")){
            // holder.collect_shopid.setText(entity.getShop_id());
            holder.collect_name.setText(entity.getShopname());
            Picasso.with(context).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到店铺详细页
                    String str = String.valueOf(entity.getShop_id());
                    Intent intent = new Intent(context,FoodListActivity.class);
                    intent.putExtra("shop_id",str);
                    context.startActivity(intent);
                }
            });

        }else {
            // holder.collect_foodid.setText(entity.getFood_id());
            holder.collect_name.setText(entity.getFoodname());
            Picasso.with(context).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到菜谱详细页
                    String str = String.valueOf(entity.getFood_id());
                    String str1 = String.valueOf(entity.getFoodname());
                    Intent intent = new Intent(context,FoodInfoActivity.class);
                    intent.putExtra("shop_id",entity.getShop_id());
                    intent.putExtra("food_id",str);
                    intent.putExtra("foodname",str1);
                    context.startActivity(intent);
                }
            });
        }

    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView collect_pic;
        private TextView collect_name;

        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic=(ImageView)itemView.findViewById(R.id.collect_image);
            collect_name=(TextView) itemView.findViewById(R.id.collect_name);
        }
    }
    @Override
    public int getItemCount() {
        if (data==null)
            return 0;
        else
            return data.size();
    }
}
